<!--综合业务-员工信息-->
<template>
  <div class="worker-information-box">
    <div class="query" v-if="!auditStatus && auditStatus != 1">
      <view class="uni-form-item uni-column">
        <view class="title">工牌号（后五位）</view>
        <input class="uni-input" ref="jobNumber" v-model="jobNumber" focus confirm-type="search" @confirm="onQuery" maxlength="5" placeholder="请输入员工工牌号（后五位）" />
        <!-- <view class="btn" @click="onQuery"><tui-icon name="search" size="20" color="#fff"></tui-icon>查询</view> -->
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">图形验证码</view>
        <input class="uni-input" ref="jobNumber" v-model="inputCaptcha" maxlength="6" placeholder="请输入图形验证码" />
        <view class="imgBox" @click="getimg">
          <img :src="img" />
        </view>
      </view>
      <div class="error" v-show="showTips"><tui-icon name="about" size="20" color="#f60"></tui-icon>{{ text }}</div>
      <view class="queryBtn uni-column">
        <view class="btn" @click="onQuery"><tui-icon name="search" size="20" color="#fff"></tui-icon>查询</view>
      </view>
    </div>
    <WorkCard v-else :info="formData" :headerImg="headerImg"></WorkCard>
    <tui-tips backgroundColor="rgba(255, 102, 0, 0.3)" color="#f60" ref="toast"></tui-tips>
  </div>
</template>

<script>
import WorkCard from "../components/workCard.vue";
export default {
  name: "",
  components: {
    WorkCard,
  },
  data() {
    return {
      jobNumber: null,
      inputCaptcha: null,
      img: "",
      formData: {
        id: null,
        openid: "",
        jobNumber: "",
        position: "",
        name: "",
        phone: "",
        imgPath: "",
      },
      headerImg: null,
      showTips: false,
      auditStatus: null,
      text: "",
      disabled: false,
    };
  },
  watch: {
    jobNumber(newValue, oldValue) {
      if (!newValue) {
        this.showTips = false;
      }
    },
  },
  async onShow() {
    this.getimg();
  },
  async onLoad(e) {
    var vm = this;
  },
  created() {},
  methods: {
    async getimg() {
      var vm = this,
        data = [];
      if (vm.disabled) {
        let options = {
          msg: "请勿频繁请求验证码",
          duration: 1000,
        };
        vm.$refs.toast && vm.$refs.toast.showTips(options);
        return;
      }
      vm.disabled = true;
      setTimeout(() => {
        vm.disabled = false;
      }, 3000);
      vm.tui
        .request("/api/workcard/captcha", "POST", {}, false, false, true)
        .then((res) => {
          vm.img = res;
        })
        .catch((e) => {});
    },
    arrayBufferToBase64(buffer) {
      let binary = "";
      const bytes = new Uint8Array(buffer);
      const length = bytes.byteLength;
      for (let i = 0; i < length; i++) {
        binary += String.fromCharCode(bytes[i]);
      }
      return window.btoa(binary);
    },
    onQuery() {
      var vm = this,
        data = {};
      if (!vm.jobNumber) {
        vm.text = "请输入工牌号（最后五位）";
        vm.showTips = true;
        return;
      }
      if (vm.jobNumber.length != 5) {
        // vm.$refs?.jobNumber?.$children[0]?.focus();
        vm.text = "工牌号必须五位数（最后五位）";
        vm.showTips = true;
        return;
      }
      if (!vm.inputCaptcha) {
        vm.text = "请输入图形验证码";
        vm.showTips = true;
        return;
      }
      vm.showTips = false;
      vm.tui
        .request("/api/workcard/list_h5", "post", { jobNumber: vm.jobNumber, inputCaptcha: vm.inputCaptcha }, false, false, false)
        .then((res) => {
          if (res.code == 200) {
            if (res.data && res.data.length === 1) {
              data = res.data[0];
              vm.auditStatus = data.status;
              if (vm.auditStatus == 1) {
                vm.formData = {
                  id: data.id,
                  jobNumber: data.jobNumber,
                  position: data.position,
                  name: data.name,
                  phone: data.phone,
                  imgPath: data.imgPath,
                };
                vm.headerImg = data.imgPath;
              } else {
                vm.text = "暂未查询到相关工号的员工信息";
                vm.showTips = true;
              }
            } else if (res.message) {
              vm.text = res.message;
              vm.showTips = true;
              if (res.message === '验证码错误！') {
                vm.getimg();
              }
            } else {
              vm.text = "暂未查询到相关工号的员工信息";
              vm.showTips = true;
            }
          }
        })
        .catch((e) => {});
    },
  },
  mounted() {
    var vm = this;
  },
};
</script>

<style lang="scss" scoped>
@import "../../static/css/module.scss";
.worker-information-box {
  width: 100%;
  height: 100%;
  .query {
    padding: 30px 14px;
    .uni-form-item {
      position: relative;
      margin-bottom: 10px;
      .title {
        font-size: 15px;
        color: #222;
        line-height: 40px;
      }
      .uni-input {
        height: 40px;
        line-height: 40px;
        background: #fff;
        font-size: 15px;
        padding: 0 14px;
      }
      .imgBox {
        width: 150px;
        height: 40px;
        position: absolute;
        bottom: 0px;
        right: 0;
        background: #f2f2f2;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .btn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 80px;
        line-height: 38px;
        position: absolute;
        bottom: 1px;
        right: 0;
        background: rgb(20, 98, 255);
        font-size: 15px;
        color: #fff;
        .tui-icon {
          padding-right: 4px;
        }
      }
    }
    .queryBtn {
      margin-top: 50px;
      .btn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        line-height: 44px;
        background: rgb(20, 98, 255);
        border-radius: 6px;
        font-size: 15px;
        color: #fff;
        .tui-icon {
          padding-right: 4px;
        }
      }
    }
    .error {
      padding: 8px 0 0;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      color: #f60;
      font-size: 15px;
      .tui-icon {
        padding-right: 4px;
      }
    }
  }
}
</style>
<style>
page {
  width: 100%;
  height: 100%;
}
</style>
